/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa036is.controllers;

import cz.muni.fi.pa036is.dto.AdmissionFormTO;
import cz.muni.fi.pa036is.dto.LoginDTO;
import cz.muni.fi.pa036is.dto.PersonDetailTO;
import cz.muni.fi.pa036is.servicesinterface.AdmissionService;
import cz.muni.fi.pa036is.servicesinterface.PersonService;
import cz.muni.fi.pa036is.servicesinterface.SeasonService;
import cz.muni.fi.pa036is.validators.AdmissionValidator;
import java.util.HashMap;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

/**
 *
 * @author jostri
 */
@Controller
@SessionAttributes("admission")
public class AdmissionController {

	@Autowired
	private AdmissionService admissionService;
	@Autowired
	private PersonService personService;
	@Autowired
	private SeasonService seasonService;

	@RequestMapping(value = "/admin/admission/edit", params = {"personId", "seasonId"})
	public String edit(@RequestParam("personId") Long personId, @RequestParam("seasonId") Long seasonId, ModelMap model) {

		PersonDetailTO person = this.personService.getPersonDetail(personId);
		
		AdmissionFormTO admission = null;
		if (seasonId == null || seasonId < 0) {
			admission = new AdmissionFormTO();
			admission.setPersonId(personId);
			admission.setPersonFirstName(person.getFirstName());
			admission.setPersonLastName(person.getLastName());
		} else {
			admission = this.admissionService.getAdmissionForm(personId, seasonId);
		}

		model.addAttribute("admission", admission);
		model.addAttribute("seasons", this.seasonService.getAllSeasonsForDropDown());

		return "/admin/admission/edit";
	}

	@RequestMapping(value = "/admin/admission/edit", method = RequestMethod.POST)
	public String edit(@Validated @ModelAttribute("admission") AdmissionFormTO admission,
						BindingResult result, ModelMap model,
			HttpSession session) {
		
            AdmissionValidator admissionValidator = new AdmissionValidator();
            admissionValidator.validate(admission, result);
       
		if (result.hasErrors()) {
                    HashMap<String, String> errors = new HashMap<String, String>();
                for (FieldError error : result.getFieldErrors()) {
                    errors.put(error.getField(), error.getDefaultMessage());
                }
            model.put("errors", errors);
			model.put("admission", admission);
                        model.addAttribute("seasons", this.seasonService.getAllSeasonsForDropDown());
			return "/admin/admission/edit";
		}

		if (admission.getId() == null || admission.getId() < 0) {
			this.admissionService.addAdmission(admission);
		} else {
			this.admissionService.editAdmission(admission);
		}


		return "redirect:/admin/person/detail?id=" + admission.getPersonId();
	}
	
	@RequestMapping(value = "/admin/admission/remove", params = { "personId", "seasonId" })
	public String remove(@RequestParam("personId") Long personId,@RequestParam("seasonId") Long seasonId, ModelMap model, HttpSession session) {

		this.admissionService.deleteAdmission(personId, seasonId);
		return "redirect:/admin/person/detail?id=" + personId;
	}
}
